#include "git-compat-util.h"
#include "hash.h"
#include "hex.h"

/**
 * Parse a null-terminated hexadecimal string into a hash using the given hash
 * algorithm.
 *
 * @param hex Pointer to the null-terminated hexadecimal string to parse.
 * @param hash Pointer to buffer to receive the parsed hash.
 * @param algop Pointer to the hash algorithm to use.
 * @return 0 on success, non-zero on failure.
 */
static int get_hash_hex_algop(const char *hex, unsigned char *hash,
			      const struct git_hash_algo *algop)
{
	int i;
	for (i = 0; i < algop->rawsz; i++) {
		int val = hex2chr(hex);
		if (val < 0)
			return -1;
		*hash++ = val;
		hex += 2;
	}
	return 0;
}

/**
 * Parse a null-terminated hexadecimal string into a SHA1 hash.
 *
 * @param hex Pointer to the null-terminated hexadecimal string to parse.
 * @param sha1 Pointer to 20-byte buffer to receive the parsed SHA1 hash.
 * @return 0 on success, non-zero if string contains invalid characters.
 */
int get_hash_hex(const char *hex, unsigned char *sha1)
{
	return get_hash_hex_algop(hex, sha1, the_hash_algo);
}

/**
 * Parse a null-terminated hexadecimal string into an object ID using
 * the given hash algorithm, stopping at the first invalid character.
 *
 */
int get_oid_hex_algop(const char *hex, struct object_id *oid,
		      const struct git_hash_algo *algop)
{
	int ret = get_hash_hex_algop(hex, oid->hash, algop);
	if (!ret)
		oid_set_algo(oid, algop);
	return ret;
}

/*
 * NOTE: This function relies on hash algorithms being in order from shortest
 * length to longest length.
 */
/**
 * Parse a null-terminated hexadecimal string into an object ID using
 * any hash algorithm, stopping at the first invalid character.
 *
 */
int get_oid_hex_any(const char *hex, struct object_id *oid)
{
	int i;
	for (i = GIT_HASH_NALGOS - 1; i > 0; i--) {
		if (!get_oid_hex_algop(hex, oid, &hash_algos[i]))
			return i;
	}
	return GIT_HASH_UNKNOWN;
}

/**
 * Parse a null-terminated hexadecimal string into an object ID.
 *
 * @param hex Pointer to the null-terminated hexadecimal string to parse.
 * @param oid Pointer to the object ID struct to populate from the string.
 * @return 0 on success, non-zero if string contains invalid characters.
 */
int get_oid_hex(const char *hex, struct object_id *oid)
{
	return get_oid_hex_algop(hex, oid, the_hash_algo);
}

/**
 * Parse a null-terminated hexadecimal string into an object ID using
 * the given hash algorithm, stopping at the first invalid character.
 *
 * @param hex Pointer to the null-terminated hexadecimal string to parse.
 * @param oid Pointer to the object ID struct to populate from the string.
 * @param end Returns a pointer to the first invalid character.
 * @param algop Pointer to the git_hash_algo for the algorithm to use.
 * @return 0 on success, non-zero if string contains invalid characters.
 */
int parse_oid_hex_algop(const char *hex, struct object_id *oid,
			const char **end,
			const struct git_hash_algo *algop)
{
	int ret = get_oid_hex_algop(hex, oid, algop);
	if (!ret)
		*end = hex + algop->hexsz;
	return ret;
}

/**
 * Parse a null-terminated hexadecimal string into an object ID using
 * any hash algorithm, stopping at the first invalid character.
 *
 * @param hex Pointer to the null-terminated hexadecimal string to parse.
 * @param oid Pointer to the object ID struct to populate from the string.
 * @param end Returns a pointer to the first invalid character.
 * @return Index of matching hash algorithm if parse succeeded,
 *         GIT_HASH_UNKNOWN if no algorithm matched.
 */
int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end)
{
	int ret = get_oid_hex_any(hex, oid);
	if (ret)
		*end = hex + hash_algos[ret].hexsz;
	return ret;
}

/**
 * Parse a null-terminated hexadecimal string into an object ID,
 * stopping at the first invalid character.
 *
 * @param hex Pointer to the null-terminated hexadecimal string to parse.
 * @param oid Pointer to the object ID struct to populate from the string.
 * @param end Returns a pointer to the first invalid character.
 * @return 0 on success, non-zero if string contains invalid characters.
 */
int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
{
	return parse_oid_hex_algop(hex, oid, end, the_hash_algo);
}

/**
 * Convert a hash value to a null-terminated hexadecimal string
 * using the provided hash algorithm, writing into the given buffer.
 *
 * @param buffer Pointer to character buffer to write hexadecimal string into.
 * @param hash Pointer to the hash value byte array to convert.
 * @param algop Pointer to the git_hash_algo struct for the
 *        hash algorithm to use.
 */
char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash,
			  const struct git_hash_algo *algop)
{
	static const char hex[] = "0123456789abcdef";
	char *buf = buffer;
	int i;

	/*
	 * Our struct object_id has been memset to 0, so default to printing
	 * using the default hash.
	 */
	if (algop == &hash_algos[0])
		algop = the_hash_algo;

	for (i = 0; i < algop->rawsz; i++) {
		unsigned int val = *hash++;
		*buf++ = hex[val >> 4];
		*buf++ = hex[val & 0xf];
	}
	*buf = '\0';

	return buffer;
}

/**
 * Convert object ID to hexadecimal string, writing into the provided buffer.
 *
 * @param buffer Pointer to character buffer to write hexadecimal string into.
 * @param oid Pointer to object ID struct to convert.
 * @return Pointer to the supplied buffer containing the null-terminated
 * hexadecimal string.
 */
char *oid_to_hex_r(char *buffer, const struct object_id *oid)
{
	return hash_to_hex_algop_r(buffer, oid->hash, &hash_algos[oid->algo]);
}

/**
 * Convert a hash value to a null-terminated hexadecimal string
 * using the provided hash algorithm.
 *
 * @param hash Pointer to the hash value byte array to convert.
 * @param algop Pointer to the git_hash_algo struct for the
 *        hash algorithm to use.
 * @return Pointer to the null-terminated hexadecimal string.
 */
char *hash_to_hex_algop(const unsigned char *hash,
			const struct git_hash_algo *algop)
{
	static int bufno;
	static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
	bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
	return hash_to_hex_algop_r(hexbuffer[bufno], hash, algop);
}

/**
 * Convert a hash value to a null-terminated hexadecimal string.
 *
 * @param hash Pointer to the hash value byte array to convert.
 * @return Pointer to the null-terminated hexadecimal string.
 */
char *hash_to_hex(const unsigned char *hash)
{
	return hash_to_hex_algop(hash, the_hash_algo);
}

/**
 * Convert object ID to hexadecimal string.
 *
 * @param oid Pointer to object ID struct.
 * @return Pointer to buffer containing null-terminated hexadecimal string.
 */
char *oid_to_hex(const struct object_id *oid)
{
	return hash_to_hex_algop(oid->hash, &hash_algos[oid->algo]);
}
